/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.compare');
(function () {
	var DQ = Ext.DomQuery;
	
	var OEMImage = Ext.extend(Ext.Component, {
		acode: undefined,
		
		urlPattern: '{0}/{1}.jpg',
		
		autoEl: {
			tag: 'img',
			cls: 'img-oem-logo',
			src: Ext.BLANK_IMAGE_URL
		},
		
		setAcode: function (acode)
		{	
			this.acode = acode;
		},
		
		setImageSrc: function()
		{
			if (Ext.isEmpty(this.acode) || !this.rendered) {
				return;
			}
			
			var dir = ASC.cfg.getOEMLogosPath();
			var division = this.acode.substr(5, 2);
			
			this.el.set({src: String.format(this.urlPattern, dir, division)});
		},
				
		onRender: function ()
		{
			OEMImage.superclass.onRender.apply(this, arguments);
			this.setImageSrc(this.src);
		}
	});
	
	/**
	 * This view class is used to define how SponsoredCompetitor VehiclePanel is rendered
	 * @class SponsoredCompetitorView
	 * @extends net.autodata.compare.AbstractView
	 * @namespace net.autodata.compare
	 */
	var View = Ext.extend(net.autodata.compare.AbstractVehicleView, {
		
		showOEMLogo: false,
		
	    vehicleInfoCfg: {
	        descriptionMaxLength: 30
	    },
		
		/**
		 * IOFImage configuration properties
		 * @property imageCfg
		 * @type Object
		 */
		imageCfg: {
			imageWidth: 138,
			imageHeight: 49
		},
		
		oemLogo: undefined,
		
		//override abstract controller to allow creation of this concret class
		constructor: function ()
		{		
			//call ctor to ensure the Base Classes are created correctly
			View.superclass.ctor.apply(this, arguments);
		},
		
		init: function ()
		{
			View.superclass.init.apply(this, arguments);
			this.oemLogo = new OEMImage();
		},
		
		/**
		 * @method initTemplates
		 */
		initTemplates: function ()
		{
			var ts = this.templates || {};
			
			if (!ts.mainTpl) {				
				ts.mainTpl = new Ext.XTemplate(					
					'<div class="sponsoredCompetitor-view">',
						'<div class="vehiclepanel-header"><div class="vehiclepanel-header-inner"><h6>{[ASC.message(\'net.autodata.compare.vehiclepanel.label.sponsored-vehicle\')]}</h6></div></div>',
						'<div class="vehiclepanel-body">',
							'<div class="vehicle-image"></div>',
							'<div class="vehicle-info"></div>',				
						'</div>',
						'<div class="oem-image"></div>',
					'</div>',
					{
						compiled: true,
						disableFormats: true
					}
				);
			}
						
			this.templates = ts;
			
			View.superclass.initTemplates.apply(this, arguments);
		},
		
		/**
		 * @method render
		 */
		render: function ()
		{
			View.superclass.render.apply(this, arguments);
			
			var c = this.component;
			var dom = c.el.dom;
			
			this.templates.mainTpl.overwrite(dom, {});

			this.vehicleInfo.render(DQ.selectNode('.vehicle-info', dom));
			this.vehicleImage.render(DQ.selectNode('.vehicle-image', dom));
			
			if (this.showOEMLogo) {
				this.oemLogo.setAcode(c.getVehicle().acode);
				this.oemLogo.render(DQ.selectNode('.oem-image', dom));
			}
		}		
	});
	
	net.autodata.compare.SponsoredCompetitorView = View;	
})();